import config from '@/config/env'
import axios from 'axios'
import qs from 'qs'
import { Toast } from 'vant';
import Router from '@/router/index'

let baseUrl = config.walletUrl;
let axiosConfig = {
    baseURL: baseUrl,
    headers: {
        'Content-Type': 'application/json'
    },
    timeout: 30000
}
var instance = axios.create(axiosConfig)

function userFunc() {
    let user = null
    if (localStorage.getItem('userInfor')) {
        user = JSON.parse(localStorage.getItem('userInfor'))
    }
    return user
}

var service = {
    //  与服务器进行post交互
    async post(api, sendData, format) {
        let user = userFunc()
        if (user && user.token) {
            instance.defaults.headers['Authorization'] = user.token
        }
        sendData = sendData || {}
        let promise = new Promise(function(resolve, reject) {
            instance
                .post(api, sendData)
                .then((response) => {
                    if (response && (response.status === 200 || response.status === 201)) {
                        let data = response.data
                        if (Number(data.status) === 200) {
                            resolve(data)
                        } else if (Number(data.status) === 401) {
                            Router.push({ path: '/login' })
                            Toast('登录失效，请重新登录')
                            sessionStorage.clear()
                            localStorage.removeItem('userInfor');
                            resolve(false)
                        } else {
                            Toast(data.msg)
                            resolve(false)
                        }
                    } else if (!response) {
                        Toast('请求超时，请稍后再试.')
                        resolve(false)
                    } else {
                        Toast('请求失败')
                        resolve(false)
                    }
                })
                .catch((error) => {
                    if (error.response.data.status == 401) {
                        Router.push({ path: '/login' })
                        Toast('登录失效，请重新登录')
                        sessionStorage.clear()
                        localStorage.removeItem('userInfor');
                    } else if (error.response && error.response.data && error.response.data.msg) {
                        Toast(error.response.data.msg)
                    }
                    resolve(false)
                })
        })
        return promise
    },
    get(api, sendData) {
        let user = userFunc()
        sendData = sendData || {}
        if (user && user.token) {
            instance.defaults.headers['Authorization'] = user.token
        }
        let promise = new Promise(function(resolve, reject) {
            instance
                .get(api, {
                    params: sendData
                })
                .then((response) => {
                    if (response && response.status === 200) {
                        let data = response.data
                        if (Number(data.status) === 200) {
                            resolve(data)
                        } else if (Number(data.status) === 401) {
                            Router.push({ path: '/login' })
                            Toast('登录失效，请重新登录')
                            sessionStorage.clear()
                            localStorage.removeItem('userInfor');
                            resolve(false)
                        } else {
                            Toast(data.Toast)
                            resolve(false)
                        }
                    } else if (!response) {
                        Toast('请求超时，请稍后再试.')
                        resolve(false)
                    } else {
                        Toast('请求失败')
                        resolve(false)
                    }
                })
                .catch((error) => {
                    resolve(false)
                })
        })

        return promise
    }
}

export default service